package cqrtplm.aop;

import cn.hutool.core.collection.CollUtil;
import com.hustcad.plm.pdm.common.model.vo.iteration.CommonIteratorVO;
import com.hustcad.plm.pdm.doc.service.TyplmDocumentService;
import com.hustcad.plm.pdm.iteration.service.TyplmIterationService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartDescribeLinkService;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.hustcad.plm.pdm.partbom.vo.QueryDescribeObjVO;
import com.ty.basic.entity.IdentifierEntity;
import cqrtplm.mapper.StoragePlanningMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class BatchNewVersionWithPolicyAOP{
    @Resource
    private StoragePlanningMapper storagePlanningMapper;

    @Resource
    private TyplmDocumentService typlmDocumentService;
    @Resource
    private TyplmPartDescribeLinkService typlmPartDescribeLinkService;

    @Resource
    private TyplmIterationService typlmIterationService;


    @Around(value = "execution(* com.hustcad.plm.pdm.iteration.controller.TyplmIterationController.batchNewVersionWithPolicy(..))")
    public Object batchNewVersionWithPolicy(ProceedingJoinPoint point) throws Throwable {
        List<CommonIteratorVO> list =(List<CommonIteratorVO>) point.getArgs()[0];

        for (CommonIteratorVO commonIteratorVO : list) {
            if (commonIteratorVO.getTypeDefName().equals("ty.inteplm.doc.CTyDoc")){
                String id = commonIteratorVO.getId();
                Map<String, String> map = storagePlanningMapper.selectDocType(commonIteratorVO.getId());
                String typeName = map.get("DOCTYPE");
                IdentifierEntity identifierEntity = new IdentifierEntity();
                identifierEntity.setOid(new BigInteger(id));
                identifierEntity.setOtype("ty.inteplm.doc.CTyDoc");
                List<PartDetailsVO> partDetailsVOS = this.typlmDocumentService.queryDocReferencePartByDoc(
                        new BigInteger(id), typeName);
                List<QueryDescribeObjVO> queryDescribeObjVOS = this.typlmPartDescribeLinkService.queryDocDescribePart(
                        Arrays.asList(identifierEntity));
                List<CommonIteratorVO> commonIteratorVOList=new ArrayList();
                if (CollUtil.isNotEmpty(partDetailsVOS)){
                    for (PartDetailsVO partDetailsVO : partDetailsVOS) {
                        CommonIteratorVO civ = new CommonIteratorVO();
                                                civ.setId(partDetailsVO.getOid());
                                                civ.setTypeDefName("ty.inteplm.part.CTyPart");
                                                civ.setChangePolicy("Change");
                                                commonIteratorVOList.add(civ);
                    }
                }
                if (CollUtil.isNotEmpty(queryDescribeObjVOS)){
                    for (QueryDescribeObjVO queryDescribeObjVO : queryDescribeObjVOS) {
                        CommonIteratorVO civ = new CommonIteratorVO();
                        civ.setId(queryDescribeObjVO.getDescribeObjList().get(0).getOid().toString());
                        civ.setTypeDefName("ty.inteplm.part.CTyPart");
                        civ.setChangePolicy("Change");
                        commonIteratorVOList.add(civ);
                    }
                }
                this.typlmIterationService.batchNewVersionWithPolicy(commonIteratorVOList);
            }
        }

        return point.proceed();
    }
}
